package com.vzoom.logdesensitization.config;

import com.vzoom.logdesensitization.DesensitizationStrategy;
import com.vzoom.logdesensitization.strategyimpl.*;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class DesensitizationStrategyHolder implements InitializingBean, ApplicationContextAware {

    @Autowired
    private DesensitizationConfig desensitizationConfig;

    private static Boolean enable = false;

    @Autowired
    private AddressDesensitizationStrategy addressDesensitizationStrategy;
    @Autowired
    private BankcardDesensitizationStrategy bankcardDesensitizationStrategy;
    @Autowired
    private BirthdayDesensitizationStrategy birthdayDesensitizationStrategy;
    @Autowired
    private CompanyDesensitizationStrategy companyDesensitizationStrategy;
    @Autowired
    private EmailDesensitizationStrategy emailDesensitizationStrategy;
    @Autowired
    private EmptyDesensitizationStrategy emptyDesensitizationStrategy;
    @Autowired
    private IdCardDesensitizationStrategy idCardDesensitizationStrategy;
    @Autowired
    private JsonDesensitizationStrategy jsonDesensitizationStrategy;
    @Autowired
    private JsonStrDesensitizationStrategy jsonStrDesensitizationStrategy;
    @Autowired
    private NameDesensitizationStrategy nameDesensitizationStrategy;
    @Autowired
    private NsrsbhDesensitizationStrategy nsrsbhDesensitizationStrategy;
    @Autowired
    private PassportDesensitizationStrategy passportDesensitizationStrategy;
    @Autowired
    private PhoneNumberDesensitizationStrategy phoneNumberDesensitizationStrategy;
    @Autowired
    private TelephoneNumberDesensitizationStrategy telephoneNumberDesensitizationStrategy;
    @Autowired
    private UserCodeDesensitizationStrategy userCodeDesensitizationStrategy;
    @Autowired
    private XmlStrDesensitizationStrategy xmlStrDesensitizationStrategy;

    private ApplicationContext applicationContext;

    private Map<String, DesensitizationStrategy> strategyMap = new HashMap<>();
    private static Map<String, DesensitizationStrategy> staticStrategyMap;

    public Map<String, DesensitizationStrategy> getStrategyMap() {
        return strategyMap;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //系统自带的
        strategyMap.put("address", addressDesensitizationStrategy);
        strategyMap.put("bankcard", bankcardDesensitizationStrategy);
        strategyMap.put("birthday", birthdayDesensitizationStrategy);
        strategyMap.put("company", companyDesensitizationStrategy);
        strategyMap.put("email", emailDesensitizationStrategy);
        strategyMap.put("empty", emptyDesensitizationStrategy);
        strategyMap.put("idCard", idCardDesensitizationStrategy);
        strategyMap.put("name", nameDesensitizationStrategy);
        strategyMap.put("json", jsonDesensitizationStrategy);
        strategyMap.put("jsonStr", jsonStrDesensitizationStrategy);
        strategyMap.put("nsrsbh", nsrsbhDesensitizationStrategy);
        strategyMap.put("passport", passportDesensitizationStrategy);
        strategyMap.put("pn", phoneNumberDesensitizationStrategy);
        strategyMap.put("tp", telephoneNumberDesensitizationStrategy);
        strategyMap.put("userCode", userCodeDesensitizationStrategy);
        strategyMap.put("xmlStr", xmlStrDesensitizationStrategy);
        //额外配置的
        Map<String, String> strategyRegister = desensitizationConfig.getStrategyRegister();
        if (strategyRegister.size() != 0) {
            for (Map.Entry<String, String> entry : strategyRegister.entrySet()) {
                strategyMap.put(entry.getKey(), applicationContext.getBean(entry.getValue(), DesensitizationStrategy.class));
            }
        }
        strategyMap = Collections.unmodifiableMap(strategyMap);
        staticStrategyMap = strategyMap;
        enable = desensitizationConfig.getEnable();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static Boolean getEnable() {
        return enable;
    }

    public static Map<String, DesensitizationStrategy> getStaticStrategyMap() {
        return staticStrategyMap;
    }
}
